package demo;

import javax.swing.*;
import java.awt.*;

public class LoginWindow {
    public static void main(String[] args) {
        // 确保外观和感觉与操作系统一致
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

    private static void createAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("登录界面");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // 设置窗口居中显示
        frame.setLocationRelativeTo(null);

        // 创建面板并设置布局
        JPanel panel = new JPanel(new BorderLayout(10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.setBackground(Color.decode("#F0F0F0")); // 设置背景颜色

        // 添加标题标签
        JLabel titleLabel = new JLabel("欢迎登录");
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0)); // 增加底部边距

        // 创建表单面板
        JPanel formPanel = new JPanel(new GridLayout(3, 1, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0));

        // 添加用户名标签和输入框
        JPanel usernamePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        JTextField usernameField = new JTextField(20);
        usernameField.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        usernameField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);

        // 添加密码标签和输入框
        JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
        passwordField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);

        // 添加登录按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        loginButton.setBackground(Color.decode("#007BFF"));
        loginButton.setForeground(Color.WHITE);
        loginButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        buttonPanel.add(loginButton);

        // 将各个面板添加到表单面板
        formPanel.add(usernamePanel);
        formPanel.add(passwordPanel);
        formPanel.add(buttonPanel);

        // 将标题标签和表单面板添加到主面板
        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(formPanel, BorderLayout.CENTER);

        // 将面板添加到窗口
        frame.add(panel);

        // 显示窗口
        frame.setVisible(true);
    }
}

